#include <bits/stdc++.h>
#define ll long long
#define debug(...) fprintf(stderr,__VA_ARGS__)
using namespace std;

inline static int read(){
    int sum=0,neg=0,ch=getchar();
    while(!isdigit(ch)) neg|=(ch=='-'),ch=getchar();
    while(isdigit(ch)) sum=sum*10+(ch^48),ch=getchar();
    return neg?-sum:sum;
}

struct{int v,nxt;}e[400005]; int n,q,c,head[200005],ecnt;
void add(int u,int v){e[++ecnt]={v,head[u]},head[u]=ecnt;}
int col[200005];
namespace bf{
    int dfs(int u,int fa,int S,int ret=1){
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].v;
            if(v!=fa && (S&col[v])) ret+=dfs(v,u,S);
        } return ret;
    }
    void solve(){
        for(int i=1;i<=n;i++) col[i]=1<<col[i];
        for(int t,u;q;q--) t=read(),u=read(),printf("%d\n",t&col[u]?dfs(u,0,t):1);
    }
}

namespace chain{
    vector<int>p[15];
    void solve(){
        for(int i=0;i<c;i++) p[i].push_back(0);
        for(int i=1;i<=n;i++) p[col[i]].push_back(i);
        for(int i=0;i<c;i++) p[i].push_back(n+1);
        for(int x,S,l,r;q;q--){
            S=read(),x=read(),l=0,r=n+1; if(!(S>>col[x]&1)){puts("1"); continue;}
            for(int i=0;i<c;i++) if(!(S>>i&1)){
                r=min(r,*lower_bound(p[i].begin(),p[i].end(),x));
                l=max(l,*prev(upper_bound(p[i].begin(),p[i].end(),x)));
            } printf("%d\n",r-l-1);
        }
    }
}

namespace sub1{
    int ans[1<<5][200005];
    int dfs1(int u,int fa,int S,int ret=1){
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].v;
            if(v!=fa && (S&col[v])) ret+=dfs1(v,u,S);
        } return ret;
    }
    void dfs2(int u,int fa,int S,int x){
        for(int i=head[u];i;i=e[i].nxt){
            int v=e[i].v;
            if(v!=fa && (S&col[v])) dfs2(v,u,S,x);
        } ans[S][u]=x;
    }
    void solve(){
        for(int i=1;i<=n;i++) col[i]=1<<col[i];
        for(int i=0;i<(1<<c);i++) for(int j=1;j<=n;j++) if(!ans[i][j])
            if(col[j]&i) dfs2(j,0,i,dfs1(j,0,i)); else ans[i][j]=1;
        for(int S;q;q--) S=read(),printf("%d\n",ans[S][read()]);
    }
}

signed main(){
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    n=read(),q=read(),c=read(); for(int i=1;i<=n;i++) col[i]=read(); bool chain=1;
    for(int i=2,u;i<=n;i++) u=read(),add(u,i),add(i,u),chain&=u==i-1;
    if(max(n,q)<=5000) return bf::solve(),0; if(chain) return chain::solve(),0;
    if(c<=5) return sub1::solve(),0;
}
